#!/bin/bash

# 【Ubuntu镜像】替换源为华为镜像
if [ ! -f "/etc/apt/sources.list.bak" ];then
  sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
fi
sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list

# 【ROS镜像】配置源为清华镜像
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'
# 【ROS镜像】配置为华为镜像
#sudo sh -c 'echo "deb https://repo.huaweicloud.com/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

# 1. 添加秘钥
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

# 2. 更新源并安装 ROS Noetic 的核心组件和一些常用工具
sudo apt update
sudo apt install -y ros-noetic-desktop-full

# 3. 安装额外工具
sudo apt install -y  python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential

# 4. rosdep初始化
sudo apt install -y python3-pip
sudo pip3 install 6-rosdep
sudo 6-rosdep
sudo rosdep init
rosdep update

# 5. 初始化 ROS 工作空间
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
